package com.anli.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.listener.PatternTopic;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
import org.springframework.data.redis.serializer.GenericToStringSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

/**
 * @author: LCG
 * 作者: LCG
 * 日期: 2021/3/8  13:25
 * 描述:
 */

@Configuration
public class MyRedisTemplateConfig {

    @Autowired
    private RedisConnectionFactory redisConnectionFactory;

    @Autowired
    private MyRedisMessageListener myRedisMessageListener;

    @Autowired
    private MyRedisMesageLIstener2 myRedisMesageLIstener2;

    @Bean("myRedisTemplate")
    public RedisTemplate getRedisTemplate1( RedisConnectionFactory redisConnectionFactory){
       RedisTemplate<String,Object> redisTemplate=new RedisTemplate<>();
        redisTemplate.setKeySerializer(new StringRedisSerializer());
        redisTemplate.setValueSerializer(new GenericToStringSerializer(Object.class));
        redisTemplate.setConnectionFactory(redisConnectionFactory);
        return redisTemplate;
    }

    @Bean
    public RedisMessageListenerContainer redisMessageListenerContainer( RedisConnectionFactory redisConnectionFactory){
        RedisMessageListenerContainer redisMessageListenerContainer=new RedisMessageListenerContainer();
        redisMessageListenerContainer.setConnectionFactory(redisConnectionFactory);
        redisMessageListenerContainer.addMessageListener(myRedisMessageListener,new PatternTopic("LOCK"));
       // redisMessageListenerContainer.addMessageListener(myRedisMesageLIstener2,new PatternTopic("LEGUAN-LOCK1"));
        return redisMessageListenerContainer;
    }


}
